using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Expression.Controls
{
	internal static class Utilities
	{
		public static Panel GetItemsHost(this ItemsControl control)
		{
			if (control == null)
			{
				throw new ArgumentNullException("control");
			}
			DependencyObject dependencyObject = control.get_ItemContainerGenerator().ContainerFromIndex(0);
			if (dependencyObject != null)
			{
				return VisualTreeHelper.GetParent(dependencyObject) as Panel;
			}
			FrameworkElement frameworkElement = Enumerable.FirstOrDefault<DependencyObject>(control.GetVisualChildren()) as FrameworkElement;
			if (frameworkElement != null)
			{
				ItemsPresenter itemsPresenter = Enumerable.FirstOrDefault<ItemsPresenter>(Enumerable.OfType<ItemsPresenter>(frameworkElement.GetLogicalDescendents()));
				if (itemsPresenter != null && VisualTreeHelper.GetChildrenCount(itemsPresenter) > 0)
				{
					return VisualTreeHelper.GetChild(itemsPresenter, 0) as Panel;
				}
			}
			return null;
		}
		internal static IEnumerable<T> TraverseBreadthFirst<T>(T initialNode, Func<T, IEnumerable<T>> getChildNodes, Func<T, bool> traversePredicate)
		{
			Utilities.<TraverseBreadthFirst>d__0<T> <TraverseBreadthFirst>d__ = new Utilities.<TraverseBreadthFirst>d__0<T>(-2);
			<TraverseBreadthFirst>d__.<>3__initialNode = initialNode;
			<TraverseBreadthFirst>d__.<>3__getChildNodes = getChildNodes;
			<TraverseBreadthFirst>d__.<>3__traversePredicate = traversePredicate;
			return <TraverseBreadthFirst>d__;
		}
		public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject element)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			return Enumerable.Skip<DependencyObject>(element.GetVisualChildrenAndSelfIterator(), 1);
		}
		private static IEnumerable<DependencyObject> GetVisualChildrenAndSelfIterator(this DependencyObject element)
		{
			Utilities.<GetVisualChildrenAndSelfIterator>d__6 <GetVisualChildrenAndSelfIterator>d__ = new Utilities.<GetVisualChildrenAndSelfIterator>d__6(-2);
			<GetVisualChildrenAndSelfIterator>d__.<>3__element = element;
			return <GetVisualChildrenAndSelfIterator>d__;
		}
		internal static IEnumerable<FrameworkElement> GetLogicalChildren(this FrameworkElement parent)
		{
			Utilities.<GetLogicalChildren>d__f <GetLogicalChildren>d__f = new Utilities.<GetLogicalChildren>d__f(-2);
			<GetLogicalChildren>d__f.<>3__parent = parent;
			return <GetLogicalChildren>d__f;
		}
		internal static IEnumerable<FrameworkElement> GetLogicalDescendents(this FrameworkElement parent)
		{
			return Utilities.TraverseBreadthFirst<FrameworkElement>(parent, (FrameworkElement node) => node.GetLogicalChildren(), (FrameworkElement node) => true);
		}
	}
}
